package com.kingbook.services;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingbook.common.utils.DateUtils;
import com.kingbook.core.base.SupportService;
import com.kingbook.core.result.JSONResult;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.kingbook.common.constant.Status.YES;

/**
 * @Author Bucky
 * @Date 2020-03-13 17:42:48
 * @Version 1.0
 */
@Slf4j
@Service
public class ReportService extends SupportService {

    @Autowired
    WorkorderService workorderService;

    @Autowired
    PartnerService partnerService;

    @PostConstruct
    private void init() {
        setNamespace("Report");
    }

    /**
     * 经营利润分析 混合图， 收入-曲线 支出-不显示 利润-柱状
     *
     * @param mid
     * @return
     */
    public JSONResult profit(int mid) {
        Map<String, Object> param = new HashMap<>(1);
        param.put("mid", mid);
        JSONResult jr = query("queryProfit", param);
        JSONObject json = new JSONObject();
        JSONArray categories = new JSONArray();
        JSONArray series = new JSONArray();
        JSONArray incomeData = new JSONArray();
        JSONArray expendData = new JSONArray();
        JSONArray profitData = new JSONArray();
        int maxAmount = 1000;
        if (jr.isSuccess() && jr.getCount() > 0) {
            int income, expend, profit;
            JSONArray array = jr.getData();
            List<String> months = DateUtils.getMonthBetween(jr.getDataElement("months"), jr.getDataElement(array.size() - 1, "months"));
            for (String month : months) {
                categories.add(month);
                boolean find = false;
                for (int i = 0; i < array.size(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    String mon = object.getString("months");
                    if (mon.equals(month)) {
                        income = object.getIntValue("income");
                        expend = object.getIntValue("expend");
                        profit = income - expend;
                        incomeData.add(income);
                        expendData.add(expend);
                        profitData.add(profit);
                        maxAmount = Math.max(income, maxAmount);
                        find = true;
                        break;
                    }
                }
                if (!find) {
                    incomeData.add(0);
                    expendData.add(0);
                    profitData.add(0);
                }
            }
        }
        JSONObject jsonIncome = new JSONObject();
        jsonIncome.put("name", "收入");
        jsonIncome.put("type", "line");
        jsonIncome.put("style", "curve");
        jsonIncome.put("addPoint", true);
        jsonIncome.put("color", "#1890ff");
        jsonIncome.put("data", incomeData);
        JSONObject jsonExpend = new JSONObject();
        jsonExpend.put("name", "支出");
        jsonExpend.put("type", "column");
        jsonExpend.put("textSize", "0");
        jsonExpend.put("color", "#ffd465");
        jsonExpend.put("data", expendData);
        JSONObject jsonProfit = new JSONObject();
        jsonProfit.put("name", "利润");
        jsonProfit.put("type", "column");
        jsonProfit.put("textSize", "0");
        jsonProfit.put("color", "#cbf17d");
        jsonProfit.put("data", profitData);
        series.add(jsonIncome);
        series.add(jsonExpend);
        series.add(jsonProfit);
        json.put("series", series);
        json.put("categories", categories);
        json.put("maxAmount", maxAmount - maxAmount % 1000 + 1000);
        return JSONResult.success(json);
    }

    /**
     * 出车排行榜 柱状图
     *
     * @param type
     * @param mid
     * @return
     */
    public JSONResult rankList(int type, int mid) {
        Map<String, Object> param = new HashMap<>(1);
        param.put("mid", mid);
        JSONResult jr = JSONResult.failed();
        if (type == 1)
            jr = query("queryRankForCustomer", param);
        else if (type == 2)
            jr = query("queryRankForPartner", param);
        else if (type == 3)
            jr = query("queryRankForAllot", param);
        JSONObject json = new JSONObject();
        JSONArray categories = new JSONArray();
        JSONArray series = new JSONArray();
        JSONArray total = new JSONArray();
        JSONArray inout = new JSONArray();
        int maxAmount = 1000;
        if (jr.isSuccess() && jr.getCount() > 0) {
            JSONArray array = jr.getData();
            int totalAmount, cash, inoutAmount;
            for (int i = 0; i < array.size(); i++) {
                JSONObject object = array.getJSONObject(i);
                categories.add(object.getString("name"));
                totalAmount = object.getIntValue("totalAmount");
                inoutAmount = object.getIntValue("inoutAmount");
                cash = object.getIntValue("cash");
                total.add(totalAmount);
                inout.add(cash + inoutAmount);
                maxAmount = Math.max(totalAmount, maxAmount);
            }
        }
        JSONObject rankTotal = new JSONObject();
        rankTotal.put("name", type == 3 ? "派车费" : "出车费");
        rankTotal.put("data", total);
//        rankTotal.put("color", "#ffd465");
        JSONObject rankInout = new JSONObject();
        rankInout.put("name", type == 3 ? "已付款" : "已收款");
        rankInout.put("data", inout);
//        rankInout.put("color", "#52c41a");
        series.add(rankTotal);
        series.add(rankInout);
        json.put("series", series);
        json.put("categories", categories);
        json.put("maxAmount", maxAmount - maxAmount % 1000 + 1000);
        return JSONResult.success(json);
    }


    /**
     * 按月份统计成本组成 饼图
     *
     * @param month
     * @return
     */
    public JSONResult expend(String month, int mid) {
        Map<String, Object> param = new HashMap<>(3);
        param.put("mid", mid);
        param.put("startDate", month + "-01");
        param.put("endDate", month + "-31");
        JSONResult jr = query("queryExpend", param);
        JSONObject json = new JSONObject();
        JSONArray series = new JSONArray();
        if (jr.isSuccess() && jr.getCount() > 0) {
            JSONArray array = jr.getData();
            for (int i = 0; i < array.size(); i++) {
                JSONObject object = array.getJSONObject(i);
                JSONObject serie = new JSONObject();
                serie.put("name", object.getString("subject"));
                serie.put("data", object.getIntValue("totalAmount"));
                series.add(serie);
            }
        }
        json.put("series", series);
        return JSONResult.success(json);
    }

    /**
     * 工单确认率
     * 30 <  #ff2816
     * 30-40 #ff5309
     * 40-50 #ff9904
     * 50-60 #ffba07
     * 60-80 #d5eb30
     * 80-100 #2fc25b
     *
     * @return
     */
    public JSONResult confirmRate(int mid) {
        Map<String, Object> param = new HashMap<>(1);
        param.put("mid", mid);
        JSONResult jr = query("queryConfirmRate", param);
        JSONObject serie1 = new JSONObject();
        JSONObject serie2 = new JSONObject();
        serie1.put("name", "伙伴用车对账率");
        serie1.put("data", "100");
        serie1.put("color", getColor(100));
        serie2.put("name", "外派工单对账率");
        serie1.put("data", "100");
        serie2.put("color", getColor(100));
        if (jr.isSuccess() && jr.getCount() > 0) {
            JSONArray array = jr.getData();
            for (int i = 0; i < array.size(); i++) {
                JSONObject object = array.getJSONObject(i);
                float data = object.getFloatValue("data") ;
                if (object.getIntValue("type") == 1) {
                    serie1.put("data", data);
                    serie1.put("color", getColor(data));
                } else {
                    serie2.put("data", data);
                    serie2.put("color", getColor(data));
                }
            }
        }
        JSONObject json1 = new JSONObject();
        JSONObject json2 = new JSONObject();
        JSONArray series1 = new JSONArray();
        JSONArray series2 = new JSONArray();
        series1.add(serie1);
        series2.add(serie2);
        json1.put("series", series1);
        json2.put("series", series2);
        JSONArray objects = new JSONArray();
        objects.add(json1);
        objects.add(json2);
        return JSONResult.success(objects);
    }

    private String getColor(float data) {
        if (data < 0.8 && data >= 0.6)
            return "#d5eb30";
        if (data < 0.6 && data >= 0.5)
            return "#ffba07";
        if (data < 0.5 && data >= 0.4)
            return "#ff9904";
        if (data < 0.4 && data >= 0.3)
            return "#ff5309";
        if (data < 0.3)
            return "#ff2816";
        return "#2fc25b";
    }
}

